package com.lottery.im.module.chat.ui;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.GiveRedPackets;
import com.lottery.im.base.BaseActivity;
import com.lottery.im.constant.Constants;
import com.lottery.im.module.chat.presenter.SendSingleRedPacketsPresenter;
import com.lottery.im.module.chat.view.SendSingleRedPacketsView;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.widget.dialog.ConfirmWarnDialogBuilder;
import com.lottery.im.widget.dialog.PassValidationDialogBuilder;

/**
 * 发送单个红包
 */
public class SendSingleRedPacketsActivity extends BaseActivity<SendSingleRedPacketsPresenter>
    implements SendSingleRedPacketsView {

  public final static String ID_KEY = "ID_KEY";

  public static void create(Activity activity, String id, int requestCode) {
    Intent intent = new Intent(activity, SendSingleRedPacketsActivity.class);
    intent.putExtra(ID_KEY, id);
    activity.startActivityForResult(intent, requestCode);
  }

  @BindView(R.id.et_money) EditText mEtMoney;
  @BindView(R.id.et_note) EditText mEtNote;
  @BindView(R.id.layout_content) RelativeLayout mLayoutContent;

  private String receivedUserId;

  @Override protected int getLayoutResId() {
    return R.layout.activity_send_singleredpackets;
  }

  @Override protected void initPresenter() {
    presenter = new SendSingleRedPacketsPresenter(this, this, this);
    presenter.init();
  }

  @Override public void initView() {
    StatusBarHelper.translucent(this);
    receivedUserId = getIntent().getStringExtra(ID_KEY);
  }

  @OnClick({ R.id.iv_back, R.id.btn_confirm, R.id.tv_record })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        finish();
        break;
      case R.id.btn_confirm:
        submit();
        break;
      case R.id.tv_record:
        NewFragmentActivity.create(this, NewFragmentActivity.FragmentInfoType.RedPacketsRecord);
        break;
    }
  }

  @OnTextChanged(value = R.id.et_money, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
  void afterTextChanged(Editable content) {

    String temp = content.toString();
    int posDot = temp.indexOf(".");
    if (posDot <= 0) return;
    if (temp.length() - posDot - 1 > 2) {
      content.delete(posDot + 3, posDot + 4);
    }
  }

  private void submit() {

    String money = mEtMoney.getText().toString();
    if (TextUtils.isEmpty(money)) {
      showToast("请输入金额");
      return;
    }
    String note = mEtNote.getText().toString();

    if (!MyApplication.getInstance().getUserInfo().getIsSetPayPwd().equals("Y")) {
      ConfirmWarnDialogBuilder.getInstance(this).setContent("请先设置资金密码")
          .setCancel("取消", null)
          .setConfirm("去设置", v -> NewFragmentActivity.create(mActivity,
              NewFragmentActivity.FragmentInfoType.SETMONEYPWD)).show();
      return;
    }

    hideKeyBoard();
    PassValidationDialogBuilder.getInstance(mActivity).setCallBack(
        code -> presenter.giveSingleRedPackets(receivedUserId, money, note, code)).show();
  }

  @Override public void giveSuc(GiveRedPackets redPackets) {
    Intent data = new Intent();
    String note = mEtNote.getText().toString();
    if (TextUtils.isEmpty(note)){
      data.putExtra(ID_KEY,
          redPackets.getRedPacketsId() + Constants.HONGBAO_INFO + "恭喜发财，大吉大利");
    }else {
      data.putExtra(ID_KEY,
          redPackets.getRedPacketsId() + Constants.HONGBAO_INFO + mEtNote.getText().toString());
    }
    setResult(Activity.RESULT_OK, data);
    finish();
  }
}
